package com.iwomy.envpro.platform.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.envpro.common.core.util.R;
import com.iwomy.envpro.common.log.annotation.SysLog;
import com.iwomy.envpro.platform.entity.AirMonitorEquipmentEntity;
import com.iwomy.envpro.platform.entity.BasicApplicationStatusEntity;
import com.iwomy.envpro.platform.exception.PlatformException;
import com.iwomy.envpro.platform.service.AirMonitorEquipmentService;
import com.iwomy.envpro.platform.service.BasicApplicationStatusService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.envpro.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用状况
 *
 * @author pig
 * @date 2023-09-23 15:45:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicApplicationStatus" )
@Tag(description = "basicApplicationStatus" , name = "应用状况管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicApplicationStatusController {

    private final  BasicApplicationStatusService basicApplicationStatusService;

	private final AirMonitorEquipmentService airMonitorEquipmentService;


	/**
     * 分页查询
     * @param page 分页对象
     * @param basicApplicationStatus 应用状况
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('platform_basicApplicationStatus_view')" )
    public R getBasicApplicationStatusPage(@ParameterObject Page page,@ParameterObject BasicApplicationStatusEntity basicApplicationStatus) {
        LambdaQueryWrapper<BasicApplicationStatusEntity> wrapper = Wrappers.lambdaQuery(basicApplicationStatus);
        return R.ok(basicApplicationStatusService.page(page, wrapper));
    }


    /**
     * 通过id查询应用状况
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('platform_basicApplicationStatus_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(basicApplicationStatusService.getById(id));
    }

    /**
     * 新增应用状况
     * @param entity 应用状况
     * @return R
     */
    @Operation(summary = "新增应用状况" , description = "新增应用状况" )
    @SysLog("新增应用状况" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform_basicApplicationStatus_add')" )
    public R save(@RequestBody BasicApplicationStatusEntity entity) {
		BasicApplicationStatusEntity wrapperEntity = new BasicApplicationStatusEntity();
		wrapperEntity.setName(entity.getName());
		LambdaQueryWrapper<BasicApplicationStatusEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicApplicationStatusEntity one = basicApplicationStatusService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)){
			throw new PlatformException("应用状况已存在");
		}
		return R.ok(basicApplicationStatusService.save(entity));
    }

    /**
     * 修改应用状况
     * @param entity 应用状况
     * @return R
     */
    @Operation(summary = "修改应用状况" , description = "修改应用状况" )
    @SysLog("修改应用状况" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform_basicApplicationStatus_edit')" )
    public R updateById(@RequestBody BasicApplicationStatusEntity entity) {
		BasicApplicationStatusEntity wrapperEntity = new BasicApplicationStatusEntity();
		wrapperEntity.setName(entity.getName());
		LambdaQueryWrapper<BasicApplicationStatusEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicApplicationStatusEntity one = basicApplicationStatusService.getOne(wrapper);
		if(ObjectUtil.isNotNull(one)&&!one.getId().equals(entity.getId())){
			throw new PlatformException("应用状况已存在");
		}
        return R.ok(basicApplicationStatusService.updateById(entity));
    }

    /**
     * 通过id删除应用状况
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除应用状况" , description = "通过id删除应用状况" )
    @SysLog("通过id删除应用状况" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform_basicApplicationStatus_del')" )
    public R removeById(@RequestBody Long[] ids) {
		long count = airMonitorEquipmentService.count(Wrappers.<AirMonitorEquipmentEntity>lambdaQuery().in(AirMonitorEquipmentEntity::getApplicationStatusId, ids));

		if (count > 0)
			return R.failed("应用状况已被使用不允许删除");
        return R.ok(basicApplicationStatusService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param basicApplicationStatus 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('platform_basicApplicationStatus_export')" )
    public List<BasicApplicationStatusEntity> export(BasicApplicationStatusEntity basicApplicationStatus) {
        return basicApplicationStatusService.list(Wrappers.query(basicApplicationStatus));
    }

	/**
	 * @param basicApplicationStatus 查询条件
	 */
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_basicApplicationStatus_export')" )
	public R list(BasicApplicationStatusEntity basicApplicationStatus) {
		return R.ok(basicApplicationStatusService.list(Wrappers.query(basicApplicationStatus)));
	}
}